package com.example.paper.http.retrofit;

import android.util.Log;

import com.example.paper.api.Api;
import com.example.paper.http.factory.HttpImpl;
import com.example.paper.interceptor.FTokenInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class FTokenRetrofit implements HttpImpl {
    private FTokenRetrofit(){}
    private static Retrofit retrofit;

    @Override
    public Retrofit getRetrofit() {
        return retrofit;
    }

    public static class Build{

        public Build(){
            createRetrofit();
        }

        private void createRetrofit(){
            Log.e("asd","token-createRetrofit");
            HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
            httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.addInterceptor(new FTokenInterceptor());
            builder.addInterceptor(httpLoggingInterceptor);
            builder.writeTimeout(60*1000, TimeUnit.MILLISECONDS);
            builder.readTimeout(60*1000,TimeUnit.MILLISECONDS);
            builder.connectTimeout(60*1000,TimeUnit.MILLISECONDS);
            OkHttpClient build = builder.build();
            Retrofit.Builder builder1 = new Retrofit.Builder();
            builder1.baseUrl(Api.BaseUrl);
            builder1.client(build);
            builder1.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            builder1.addConverterFactory(GsonConverterFactory.create());
            retrofit = builder1.build();
        }

        public FTokenRetrofit build(){
            return new FTokenRetrofit();
        }

    }

}
